﻿using System;
using System.Linq;
using Org.Flowertwig.Media.Information;

namespace Org.Flowertwig.Media.Services.InformationService.Sources
{
    public class TheMoveDbSoruce
    {
        const string SERVICE_ADDRESS = "www.TvRage.com";
        private static string _appKey = ""; // <-- Add your tv rage key here

        public static bool IsValid
        {
            get;
            set;
        }

        static TheMoveDbSoruce()
        {
            // http://themoviedbapi.codeplex.com/documentation

            if (string.IsNullOrEmpty(_appKey))
                throw new InvalidOperationException("No 'TheMovieDb' key specified.");

            IsValid = true;
        }

        public static Movie GetMovie(string name)
        {
            try
            {
                // This is to make sure we are using the same name
                name = name.ToLower();
                name = name.Replace('.', ' ');

                var api = new TheMovieDB.TmdbAPI(_appKey);
                var movies = api.MovieSearch(name);

                return movies.Select(m => new Movie
                                              {
                                                  Id = Hash.CreateId(m.Name),
                                                  Name = m.Name,
                                                  Genres = m.Categories.Select(c=> c.Name).ToArray()
                                              }).FirstOrDefault();
            }
            catch (System.Exception ex)
            {
            }
            return null;
        }
    }
}
